
package lumingweihua.future.cn.baselibgxh.utils;
/**
 * MD5
 *
 * @author Jupiter-Z (2014-11-06 14:16)
 */

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    // MD5変換
    public static String Md5(String str) {
            byte[] hash;
            try {
                hash = MessageDigest.getInstance("MD5").digest(str.getBytes("UTF-8"));
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
                return null;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
            StringBuilder hex = new StringBuilder(hash.length * 2);
            for (byte b : hash) {
                if ((b & 0xFF) < 0x10) hex.append("0");
                hex.append(Integer.toHexString(b & 0xFF));
            }
            return hex.toString().toUpperCase();
//		if (str != null && !str.equals("")) {
//			try {
//				MessageDigest md5 = MessageDigest.getInstance("MD5");
//				char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
//				byte[] md5Byte = md5.digest(str.getBytes("UTF8"));
//				StringBuffer sb = new StringBuffer();
//				for (int i = 0; i < md5Byte.length; i++) {
//					sb.append(HEX[(int) (md5Byte[i] & 0xff) / 16]);
//					sb.append(HEX[(int) (md5Byte[i] & 0xff) % 16]);
//				}
//				str = sb.toString();
//			} catch (NoSuchAlgorithmException e) {
//			} catch (Exception e) {
//			}
//		}
//		return str;
    }

}
